/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.shell;

import ecom.beans.EcomAdminRemote;
import ecom.beans.EcomCustomerRemote;
import ecom.beans.EuroConvertorBean.Currency;
import ecom.client.InitDataBase;
import ecom.shell.EcomShellConstantes;
import ecom.shell.EcomShellConstantes;
import ecom.shell.commandsImpl.LanguageCommandsImpl;
import ecom.shell.commandsImpl.OutputCommandImpl;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;
import shell.Shell;
import shell.ShellContext;

/**
 *
 * @author DE SILVA
 */
public class EcomShell extends Shell implements Runnable, EcomShellConstantes {

    @EJB
    private static EcomAdminRemote adminBean;
    @EJB
    private static EcomCustomerRemote customerBean;
    private Context initialContext;
    private UserTransaction userTransaction;

    public EcomShell(String args[], boolean isAdmin) {
        super(args);
        Properties props = new Properties();
        InitialContext ic;
        try {
            ic = new InitialContext(props);
            userTransaction = (UserTransaction) ic.lookup("java:comp/UserTransaction");
            customerBean = (EcomCustomerRemote) ic.lookup(EcomCustomerRemote.class.getName());
            adminBean = (EcomAdminRemote) ic.lookup(EcomAdminRemote.class.getName());
            try {
           //     InitDataBase.initDatabase(ic, userTransaction, adminBean, customerBean);
            } catch (Exception ex) {
                System.err.println("Error with InitDatabase : " + ex.getMessage());
            }
        } catch (NamingException ex) {
            Logger.getLogger(EcomShell.class.getName()).log(Level.SEVERE, null, ex);
        }


        ShellContext context = getContext();

        addCommand(new ecom.shell.transaction.BeginTxCommandImpl(context));
        // Add "commit" command.
        addCommand(new ecom.shell.transaction.CommitCurrentTxCommandImpl(context));
        // Add "rollback" command.
        addCommand(new ecom.shell.transaction.RollbackCurrentTxCommandImpl(context));
        // Add "account" command.
        addCommand(new ecom.shell.commandsImpl.AccountCommandImpl(context));
        // Add "product" command.
        addCommand(new ecom.shell.commandsImpl.ProductCommandImpl(context));
        // Add "productstore" command.
        addCommand(new ecom.shell.commandsImpl.ProductstoreCommandImpl(context));
        // Add "productcategory" command.
        addCommand(new ecom.shell.commandsImpl.ProductcategoryCommands(context));
        // Add "user" command.
        addCommand(new ecom.shell.commandsImpl.UserCommandImpl(context));
        // Add "cart" command.
        addCommand(new ecom.shell.commandsImpl.CartCommandImpl(context));
        // Add "stock" command.
        addCommand(new ecom.shell.commandsImpl.StockCommandImpl(context));
        // Add "quit" command.
        addCommand(new shell.SimpleQuitCommandImpl(context));

        // Add "currency" command.
        addCommand(new ecom.shell.commandsImpl.CurrencyCommandImpl(context));
        // Add "output" command.
        addCommand(new ecom.shell.commandsImpl.OutputCommandImpl(context));

        context.setVar(CURRENT_TX, null);
        context.setVar(BANNER, getBanner());
        context.setVar("adminBean", adminBean);
        context.setVar("customerBean", customerBean);
        context.setVar("currency", Currency.getDefault().name());
        context.setVar("language", LanguageCommandsImpl.Languages.getDefault());
        context.setVar("output", OutputCommandImpl.Outputs.getDefault());
        context.setVar("isAdmin", isAdmin);
        context.setVar("userTransaction", userTransaction);

    }

    protected String getBanner() {
        StringBuilder sb = new StringBuilder();
        sb.append("\n---------------------------------------------------------");
        sb.append("\n| Commerce en Ligne |");
        sb.append("\n| Miage Descartes  |");
        sb.append("\n---------------------------------------------------------");
        return sb.toString();
    }
}
